﻿
using System;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Convertor.Base;
using zijian666.Converts.Core;
using zijian666.Converts.Extensions;

namespace zijian666.Converts.Convertor;

/// <summary>
/// <seealso cref="UIntPtr"/> 转换器
/// </summary>
public class UIntPtrConvertor : FromConvertor<UIntPtr>
                              , IFrom<uint, UIntPtr>
                              , IFrom<ulong, UIntPtr>
                              , IFrom<object, UIntPtr>
{
    /// <inheritdoc/>
    public ConvertResult<UIntPtr> From(IConvertContext context, ulong input) => new UIntPtr(input);

    /// <inheritdoc/>
    public ConvertResult<UIntPtr> From(IConvertContext context, uint input) => new UIntPtr(input);

    /// <inheritdoc/>
    public ConvertResult<UIntPtr> From(IConvertContext context, object input)
    {
        var result = context.Convert<ulong>(input);
        if (result.Success)
        {
            return new UIntPtr(result.Value);
        }
        return context.ConvertFail(this, input, result.Exception);
    }
}
